<template>
  <div>
    <div class="box-container">
      <div class="header-box">
        <div class="left">
          <el-tabs v-model="queryParams.fileType" @tab-click="handleQuery">
            <el-tab-pane
              v-for="(item, index) in fileTypeList"
              :key="index"
              :label="item.name"
              :name="item.id + ''"
            ></el-tab-pane>
          </el-tabs>
        </div>
        <div class="right">
          <el-input
            placeholder="搜索"
            v-model="queryParams.title"
            prefix-icon="el-icon-search"
            @input="handleQuery"
          ></el-input>
        </div>
      </div>
    </div>
    <div class="box-container">
      <div v-if="dataList.length > 0">
        <div
          style="
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            width: 100%;
          "
        >
          <span style="min-width: 80px">学习完成度：</span>
          <el-progress
            style="width: 371px; margin: 0 10px"
            :percentage="progress"
            :stroke-width="10"
            :text-inside="false"
            :color="'#A52529'"
          >
          </el-progress>
        </div>
        <el-row :gutter="15" v-if="queryParams.fileType == '1'">
          <el-col :span="6" v-for="(item, index) in dataList" :key="index">
            <div class="item-box" @click="handleDetail(item)">
              <div class="tag">
                <el-tag
                  size="mini"
                  v-if="item.progress === 100"
                  effect="dark"
                  type="success"
                  >已学习</el-tag
                >
                <el-tag
                  size="mini"
                  v-else-if="item.progress === 0"
                  effect="dark"
                  type="info"
                  >未学习</el-tag
                >
                <el-tag size="mini" v-else effect="dark" type="info"
                  >学习中</el-tag
                >
              </div>
              <video
                ref="myVideo"
                :src="
                  item.attachment.indexOf('/profile') > -1
                    ? baseUrl + item.attachment
                    : item.attachment
                "
                width="100%"
                height="150"
              ></video>
              <div class="play">
                <i class="el-icon-caret-right"></i>
              </div>
              <div class="title">{{ item.title }}</div>
            </div>
          </el-col>
        </el-row>
        <el-table :data="dataList" v-if="queryParams.fileType == '2'">
          <el-table-column label="标题" align="center" prop="title" />
          <el-table-column label="信息来源" align="center" prop="source" />
          <el-table-column label="发布时间" align="center" prop="releaseTime" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-document"
                @click="handleDetail(scope.row)"
                >查看详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-download"
                @click="handleDownload(scope.row)"
                >下载文档
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          :page-sizes="[8, 16, 32]"
        />
      </div>
      <el-empty v-else description="这里什么都没有!"></el-empty>
    </div>
  </div>
</template>

<script>
import { getLastRegisterInfo } from "@/api/business/registerInfo";
import { listResourceInfo } from "@/api/business/resourceInfo";
import { getProgressResourceProgress } from "@/api/business/resourceProgress";

export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      fileTypeList: [
        { id: 1, name: "岗前培训" },
        // {id: 2, name: '文档'},
      ],
      dataList: [],
      total: 0,
      queryParams: {
        title: null,
        fileType: "1",
        isShow: 1,
        pageNum: 1,
        pageSize: 8,
      },
      intervalObj: null,
      progress: 0,
      isShow: false,
    };
  },
  created() {
    this.getLast();
  },
  methods: {
    getLast() {
      getLastRegisterInfo().then((res) => {
        if (res.data) {
          this.isShow = true;
          this.getList();
          this.getProgress();
        }
      });
    },
    getProgress() {
      getProgressResourceProgress().then((res) => {
        this.progress = Math.floor(res.data);
      });
    },
    getList() {
      listResourceInfo(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
      });
    },

    handleQuery() {
      if (this.isShow) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = this.queryParams.fileType == "1" ? 8 : 10;
        this.getList();
      }
    },
    handleDetail(item) {
      this.$router.push({
        path: "/business/resourceInfo/detail",
        query: { id: item.id },
      });
    },
    handleDownload(item) {
      window.open(this.baseUrl + item.attachment);
    },
  },
};
</script>

<style lang="scss" scoped>
.video-container {
  position: relative;
}

.custom-controls {
  position: absolute;
  bottom: 10px;
  left: 10px;
}

.header-box {
  display: flex;
  justify-content: space-between;

  .left {
    width: calc(100% - 320px);

    ::v-deep .el-tabs__item {
      font-size: 18px;
      margin-bottom: 5px;
    }
  }

  .right {
    width: 300px;

    ::v-deep input {
      border-radius: 20px !important;
    }
  }
}

.item-box {
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 10px;
  box-shadow: 5px 5px 10px 0 rgba(157, 156, 156, 0.1);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 使子元素水平居中 */

  .tag {
    position: absolute;
    top: 10px;
    right: 15px;
  }

  video {
    width: 100%;
    height: 150px;
    // object-fit: cover;
    /* 保持视频比例 */
  }

  .title {
    cursor: pointer;
    margin-top: 10px;
    width: 100%;
    text-align: center;
    /* 标题文本居中 */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .play {
    position: absolute;
    top: 55px;
    left: 50%;
    transform: translateX(-50%);
    /* 使用transform实现完美居中 */
    width: 60px;
    height: 60px;
    border-radius: 15px;
    background-color: rgba(43, 51, 63, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;

    i {
      font-size: 36px;
      color: white;
    }
  }
}

.item-box:hover {
  background-color: white;
}

.progress-box {
  position: absolute;
  right: 15px;
  top: 95px;
  z-index: 1;
  background-color: white;
  padding: 15px;
  border-radius: 15px;
  display: grid;
  text-align: center;
}
</style>
